<?php

/**
 * @author 
 * @copyright 2012
 */

class BolInfraController extends Controller{
	var $widKey = false;
	public function actionSaveQuestions(){	
        $obj = json_decode($_REQUEST['objForm']);        
        foreach($obj as $value){            			
            $mode = new tbl_preg_detalle;
            $mode->pre_preg = 'MODPR21';
            $mode->pre_tipopre = $value->tipo;
            $mode->pre_pregunta = $value->pregunta;
            $mode->pre_nivel = $value->nivel;
            $mode->pre_estado = '0';
            $mode->save();
        }
        echo "bien";		
        //print_r($obj);
	}
    public function actionSave(){
		$contItem = 0;
		$obj = json_decode($_REQUEST['objForm']);
		print_r($obj);
		
		
		/*
		$transaction = Yii::app()->db->beginTransaction();        
        try{		
		*/	/*
			$cmd = Yii::app()->db->createCommand();			
			foreach($obj->dataValue as $data){		
				$con = $cmd->select('MAX(res_id) AS cont')->from('tbl_resp_inf_respuest')->queryAll();    			
				$contItem++;
				$auxn = $auxs = '';
				$codePreg = tbl_preg_inf_detalle::model()->with('preg')->find('prd_pregunta=:postPrgta AND prd_nivel=:postNivel AND pre_modulo=:postModulo', 
					array(
						':postPrgta'=>$data->pregunta,
						':postNivel'=>$data->nivel,
						':postModulo'=>$data->modulo
					));
				$model = new tbl_resp_inf_respuest;
				$model->res_codigo = 'R'.($con[0]['cont'] + 1);
				$model->res_resp = 'BIR1';
				$model->res_resp_modulos = $data->modulo;
				$model->res_tipobol = 'BINF';
				$model->res_orden = $contItem;
				$model->res_pregunta = isset($codePreg->prd_codigo)?$codePreg->prd_codigo:'XXX';
				$model->res_column = isset($data->columna)?$data->columna:'';
				$model->res_nivel = $data->nivel;
				(is_numeric($data->respuesta))?$auxn = $data->respuesta:$auxs = $data->respuesta;
				$model->res_respnum = $auxn;
				$model->res_resptext = $auxs;
				$model->res_fecha = date("Y-m-d H:i:s");  
				$model->res_usuarios = Yii::app()->session['user'];
				$model->res_estado = 0;
				
				$this->widKey = $model->save();		
			}	
			
			$contItem = 0;		
			$cmd = Yii::app()->db->createCommand();				
			foreach($obj->dataCeros as $data){				
				$con = $cmd->select('MAX(rce_id) AS cont')->from('tbl_resp_inf_ceros')->queryAll();				
				$contItem++;
				$auxn = $auxs = '';
				$codePreg = tbl_preg_inf_detalle::model()->with('preg')->find('prd_pregunta=:postPrgta AND prd_nivel=:postNivel AND pre_modulo=:postModulo', 
					array(
						':postPrgta'=>$data->pregunta,
						':postNivel'=>$data->nivel,
						':postModulo'=>$data->modulo
					));
				
				$model = new tbl_resp_inf_ceros;
				$model->rce_codigo = 'C'.($con[0]['cont'] + 1);
				$model->rce_resp = 'BIR1';
				$model->rce_resp_modulos = $data->modulo;
				$model->rce_tipobol = 'BINF';
				$model->rce_orden = $contItem;
				$model->rce_pregunta = isset($codePreg->prd_codigo)?$codePreg->prd_codigo:'XXX';
				$model->rce_column = isset($data->columna)?$data->columna:'';
				$model->rce_nivel = $data->nivel;							
				$model->rce_fecha = date("Y-m-d H:i:s");  
				$model->rce_usuarios = Yii::app()->session['user'];
				$model->rce_estado = 0;
				
				$this->widKey = $model->save();                        			
			}
			$contItem = 0;		
			$cmd = Yii::app()->db->createCommand();				
			foreach($obj->dataEmpty as $data){							
				$con = $cmd->select('MAX(rem_id) AS cont')->from('tbl_resp_inf_empty')->queryAll();				
				$contItem++;
				$auxn = $auxs = '';
				$codePreg = tbl_preg_inf_detalle::model()->with('preg')->find('prd_pregunta=:postPrgta AND prd_nivel=:postNivel AND pre_modulo=:postModulo', 
					array(
						':postPrgta'=>$data->pregunta,
						':postNivel'=>$data->nivel,
						':postModulo'=>$data->modulo
					));
				
				$model = new tbl_resp_inf_empty;
				$model->rem_codigo = 'E'.($con[0]['cont'] + 1);
				$model->rem_resp = 'BIR1';
				$model->rem_resp_modulos = $data->modulo;
				$model->rem_tipobol = 'BINF';
				$model->rem_orden = $contItem;
				$model->rem_pregunta = isset($codePreg->prd_codigo)?$codePreg->prd_codigo:'XXX';
				$model->rem_column = isset($data->columna)?$data->columna:'';
				$model->rem_nivel = $data->nivel;							
				$model->rem_fecha = date("Y-m-d H:i:s");  
				$model->rem_usuarios = Yii::app()->session['user'];
				$model->rem_estado = 0;
				
				$this->widKey = $model->save();                        	
			}
			echo true;
			*/
		/*
		}catch(Exception $e){
            $this->widKey = false;
        }                
        if(!$this->widKey){
            $transaction->rollback();
            echo "Error system controller 'BolInfra' in the action -> Save.";
        }
		*/
    }
}
?>